<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
  <head>
<meta name="copyright" content="Copyright (c) IBM Corporation and others 2000, 2014. This page is made available under license. For full details see the LEGAL in the documentation book that contains this page." >
    <link rel="stylesheet" href="../book.css" charset="ISO-8859-1" type="text/css">
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <title>
      Extracting a new method
    </title>
  </head>
  <body>
    <h1>
      Extracting a New Method
    </h1>
    <p>
      In this section, you will improve the code of the constructor of <em>junit.framework.TestSuite</em>. 
      To make the intent of the code clearer, you will extract the code that collects test cases 
      from base classes into a new method called <em>collectInheritedTests</em>.
    </p>
    <ol>
      <li>
        In the <em>junit.framework/TestSuite.java</em> file, select the following range of
        code inside the <em>TestSuite(Class)</em> constructor:<br>
        <p><code>
        Class superClass= theClass;<br>
        Vector names= new Vector();<br>
        while(Test.class.isAssignableFrom(superClass)) {<br>
        &nbsp;&nbsp;&nbsp; Method[] methods= superClass.getDeclaredMethods();<br>
        &nbsp;&nbsp;&nbsp; for (int i= 0; i &lt; methods.length; i++) {<br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; addTestMethod(methods[i],names, theClass);<br>
        &nbsp;&nbsp;&nbsp; }<br>
        &nbsp;&nbsp;&nbsp; superClass= superClass.getSuperclass();<br>
        }</code></p>
      </li>
      <li>
        From the selection's context menu in the editor, select <strong>Refactor &gt; Extract Method...</strong>.
		<p>
		<img src="images/editor_testsuite_extract_method.png"  alt="Context menu extract method">
		</p>
      </li>
      <li>
        In the <strong>Method Name</strong> field, type <em>collectInheritedTests</em>.
		<p>
		<img src="images/extract_method_dialog.png"  alt="Extract method wizard data input page">
		</p>
      </li>
      <li>
        To preview the changes, press <strong>Preview&gt;</strong>.The preview page displays the 
        changes that will be made. Press <strong>OK</strong> to extract the method.
		<p>
		<img src="images/extract_method_preview_dialog.png"  alt="Extract method preview page">
		</p>
      </li>
      <li>
        Go to the extracted method by selecting it in the Outline view.
		<p>
		<img src="images/editor_testsuite_outline_collectinheritedmethod.png"  alt="Extracted method">
		</p>
      </li>
    </ol>
    <P class="nav_footer" id="nav_footer">Next Section: <A href="qs-9.htm">Creating a Java class</A>
</P>
    <h3 class="related">Related Concepts</h3>
       <a href="../concepts/concept-java-editor.htm">Java editor</a><br>
       <a href="../concepts/concept-refactoring.htm">Refactoring support</a>

    <h3 class="related">Related References</h3>
       <a href="../reference/preferences/ref-preferences-java.htm">Java Preferences</a>

  </body>
</html>

